<template>
	<view style=" min-height:100vh;background-color: white;padding: 20rpx;padding-bottom: 40rpx;">
		<fui-form ref="form" labelWidth="180">
			<fui-form-item asterisk bottomBorder label="推荐类型:">
				<fui-radio-group name="radio" @change="checked">
					<view class="fui-list__item flex">
						<view class="fui-align__center">
							<fui-radio value="0" :checked="gender==0"></fui-radio>
							<text class="fui-text">图片</text>
						</view>

						<view class="fui-align__center">
							<fui-radio value="1" :checked="gender==1"></fui-radio>
							<text class="fui-text">视频</text>
						</view>

					</view>
				</fui-radio-group>

			</fui-form-item>
			<fui-form-item asterisk bottomBorder label="推荐素材:">
				<template v-slot:vertical>
					<view class="">
						<view style="width: 100%;" v-show="gender==0">
							<fui-upload immediate :url="url" ref="imgUpload" @success="success" @error="error"
								@complete="complete"></fui-upload>
						</view>
						<view style="width: 100%;" v-show="gender==1">
							<fui-upload-video immediate :url="url" ref="videoUpload" @success="success" @error="error"
								@complete="complete"></fui-upload-video>

						</view>
					</view>
				</template>
			</fui-form-item>
			<fui-form-item :padding='["30rpx",0]' bottomBorder label="选择音频:">
				<template v-slot:vertical>
					<view class="" @click="toAudio">
						选择音频
					</view>
				</template>
			</fui-form-item>
			<fui-form-item :padding='["30rpx",0]' bottomBorder label="浮动文案:">
				<template v-slot:vertical>
					<fui-textarea maxlength="-1" :padding="['32rpx','0rpx']" :border-bottom="false" :border-top="false"
						placeholder="请输入详细描述..." v-model="formData.descr"></fui-textarea>
				</template>
			</fui-form-item>
			<fui-form-item :padding='["30rpx",0]' bottomBorder labelWidth="200rpx" label="选择链接:">
				<template v-slot:vertical>
					<view class="" @click="toLink">
						选择链接
					</view>
				</template>
			</fui-form-item>
		</fui-form>
		<view class="fui-btn__box">
			<fui-button text="确定发布" bold @click="submit"></fui-button>
		</view>

		<!-- 选择链接 -->
		<fui-bottom-popup :show="show" @close="closePopup(1)">
			<view class="fui-custom__wrap">
				<view class="fui-icon__close" @tap="closePopup(1)">
					<fui-icon name="close" :size="48"></fui-icon>
				</view>
				<selectLink></selectLink>
			</view>
		</fui-bottom-popup>
		<!-- 选择音频 -->
		<fui-bottom-popup :show="audioShow" @close="closePopup(2)">
			<view class="fui-custom__wrap">
				<view class="fui-icon__close" @tap="closePopup(2)">
					<fui-icon name="close" :size="48"></fui-icon>
				</view>
				<selectAudio></selectAudio>
			</view>
		</fui-bottom-popup>
	</view>
</template>

<script>
	import selectLink from '@/publicPages/selectLink.vue'
	import selectAudio from '@/publicPages/selectAudio.vue'

	export default {
		components: {
			selectLink,
			selectAudio
		},
		data() {
			return {
				audioShow: false,
				show: false,
				//上传接口地址
				url: 'https://ffa.firstui.cn/api/example/upload-file',
				//上传状态，用于保存或其他操作时做判断
				status: '',
				//上传的视频地址列表
				imgUrls: [],
				videoUrls: [],
				form: null,
				imgUpload: null,
				videoUpload: null,
				formData: {},
				gender: 0,
			};
		},
		methods: {
			toAudio() {
				this.audioShow = true
			},
			toLink() {
				this.show = true
			},
			closePopup(index) {
				if (index == 1) {
					this.show = false
				} else {
					this.audioShow = false
				}

			},
			//上传成功时触发
			success(e) {
				console.log(e)
				//e.res 为服务器返回数据
				//上传成功回调，处理服务器返回数据【此处根据实际返回数据进行处理】
				let res = JSON.parse(e.res.data.replace(/\ufeff/g, "") || "{}")
				this.status = e.status
				if (res.data.url) {
					//处理结果返回给组件 
					//res.data.url为上传成功后返回的视频地址
					//e.index为视频索引值
					// 0是图片 1是视频
					if (this.gender == 0) {
						this.$refs.imgUpload.result(res.data.url, e.index)
					}
					if (this.gender == 1) {
						this.$refs.videoUpload.result(res.data.url, e.index)
					}

				}
			},
			//上传失败时触发
			error(e) {
				this.status = e.status
			},
			//选择视频或上传完成时触发
			complete(e) {
				this.status = e.status
				// 0是图片1 是视频
				if (this.gender == 0) {
					this.imgUrls = e.urls
				}
				if (this.gender == 1) {
					this.videoUrls = e.urls
				}

				if (this.status === 'success' && e.action === 'upload') {
					// this.fui.toast('上传完成！')
					//已上传完成的视频列表 this.urls
					console.log(this.urls)
				}
			},
			checked(e) {
				this.gender = e.detail.value

			},
			onFormSubmit(e) {
				this.formData = e.detail.value
				console.log('this.formData', this.formData);

			},
			onFormReset(e) {
				this.formData = {}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fui-align__center {
		margin-right: 20rpx;
		display: flex;
		align-items: center;
	}

	.fui-icon__close {
		z-index: 5556;
		position: absolute;
		top: 24rpx;
		left: 24rpx;
	}

	.fui-text {
		margin-left: 10rpx;
	}

	::v-deep .fui-uploadv__del-wrap {
		width: 100% !important;
		margin: 0 0 0 0 !important;
	}

	::v-deep .fui-uploadv__item {
		width: 100% !important;
		margin: 0 0 0 0 !important;
	}

	::v-deep .fui-upload__video {
		width: 100% !important;
		margin: 0 0 0 0 !important;
	}

	.uni-form-item {
		padding: 15px 0;
	}

	.title {
		margin-bottom: 10px;
	}

	.group-item {
		flex-direction: row;
		margin-right: 20px;
	}

	.fui-custom__wrap {
		position: relative;
		padding-top: 60rpx;
		height: 80vh;
	}

	.btn {
		flex: 1;
	}

	.btn-submit {
		margin-right: 5px;
	}

	.btn-reset {
		margin-left: 5px;
	}

	.result {
		margin-top: 30px;
	}

	.formItem {
		background-color: white;
		padding: 20rpx;
	}

	.fui-btn__box {
		margin-top: 20rpx;

	}
</style>